<template>
    <div class="app-container calendar-list-container">
        <!-- 查询和其他操作 -->
        <div class="filter-container">
            <el-input clearable class="filter-item" style="width: 200px;" placeholder="支付通道名称" v-model="listQuery.payChannelName"></el-input>
            <el-input clearable class="filter-item" style="width: 200px;" placeholder="支付通道编号" v-model="listQuery.payChannelNo"></el-input>
            <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">查询</el-button>
            <el-button class="filter-item" type="primary" v-waves icon="el-icon-plus" @click="handleAdd">新增</el-button>
        </div> 
        <!-- 查询结果 -->
        <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>
            <el-table-column align="center" min-width="100px" label="支付通道编号" prop="payChannelNo"></el-table-column>
            <el-table-column align="center" min-width="100px" label="支付通道名称" prop="payChannelName"></el-table-column>
            <el-table-column align="center" min-width="100px" label="支付公司编号" prop="payCompanyNo"></el-table-column>
            <el-table-column align="center" min-width="100px" label="支付公司名称" prop="payCompanyName"></el-table-column>
            <el-table-column align="center" min-width="100px" label="业务类型">
                <template slot-scope="scope">
                    <span v-if="scope.row.tradeType == '00'">支付</span>
                    <span v-if="scope.row.tradeType == '01'">代付</span>
                </template>
            </el-table-column>
            <el-table-column align="center" min-width="100px" label="创建时间" prop="createTime"></el-table-column>
            <el-table-column align="center" min-width="100px" label="修改时间" prop="updateTime"></el-table-column>
            <el-table-column align="center" label="操作" width="400" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button type="primary" size="small" @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button type="primary" size="small" @click="handleDelete(scope.row)">删除</el-button>
                    <el-button type="primary" size="small" @click="handleInterface(scope.row)">接口配置</el-button>
                    <!--<el-button type="primary" size="small" @click="handleInterface(scope.row)">费率配置</el-button>-->
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="pagination-container" v-show="!listLoading">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page"
            :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        </div>

        <!-- 新增/修改 -->
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
            <el-form :rules="rules" ref="form" :model="form" status-icon label-position="left" label-width="120px" style='width: 400px; margin-left:50px;'>
                <el-form-item label="支付通道编号:" prop="payChannelNo">
                    <el-input v-if="dialogStatus === 'create'" v-model="form.payChannelNo"></el-input>
                    <el-input v-else-if="dialogStatus === 'update'" v-model="form.payChannelNo" disabled=""></el-input>
                </el-form-item>
                <el-form-item label="支付通道名称： " prop="payChannelName">
                    <el-input v-model="form.payChannelName" clearable=""></el-input>
                </el-form-item>        
                <el-form-item label="支付公司编号： " prop="payCompanyNo">
                    <el-input v-model="form.payCompanyNo" clearable=""></el-input>
                </el-form-item>
                <el-form-item label="支付公司名称： " prop="payCompanyName">
                    <el-input v-model="form.payCompanyName" clearable=""></el-input>
                </el-form-item>
                <el-form-item label="业务类型： " prop="tradeType">
                    <el-select class="filter-item" v-model="form.tradeType" placeholder="请选择" clearable="">
                      <el-option label="支付" value="00"> </el-option>
                      <el-option label="代付" value="01"> </el-option>
                    </el-select>
                </el-form-item>        
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button v-if="dialogStatus === 'create'" type="primary" @click="create('form')">保存</el-button>
                <el-button v-else-if="dialogStatus === 'update'" type="primary" @click="update('form')">保存</el-button>
            </div>
        </el-dialog>
        <!-- 接口配置 -->
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogInterface">
            <div style="margin-left:30px;">
                <el-button type="primary" size="small" @click="handleAddInterFace(form1.payChannelNo)">添加</el-button>
            </div>
            <el-form ref="form1" :model="form1" status-icon label-position="left" label-width="150px" style='width: 750px; margin-left:30px;'>
                <el-form-item label="支付通道编号: " prop="payChannelNo">
                    <el-input v-model="form1.payChannelNo" disabled=""></el-input>
                </el-form-item>
                <el-table size="small" :data="form1.list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>
                    <el-table-column align="center" min-width="80px" label="接口类型">
                        <template slot-scope="scope">
                            <span v-if="scope.row.tradeType == '00'">支付</span>
                            <span v-if="scope.row.tradeType == '01'">支付查询</span>
                            <span v-if="scope.row.tradeType == '02'">退款</span>
                            <span v-if="scope.row.tradeType == '03'">退款查询</span>
                            <span v-if="scope.row.tradeType == '04'">代付</span>
                            <span v-if="scope.row.tradeType == '05'">代付查询</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" min-width="120px" label="接口名称" prop="serviceName"></el-table-column>
                    <el-table-column align="center" min-width="100px" label="创建时间" prop="createTime"></el-table-column>
                    <el-table-column align="center" min-width="100px" label="修改时间" prop="updateTime"></el-table-column>
                    <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                            <el-button type="primary" size="small" @click="handleUpdateInterFace(scope.row)">修改</el-button>
                            <el-button type="primary" size="small" @click="handleDeleteInterFace(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-form>
        </el-dialog>

        <!-- 接口配置 -->
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogInterfaceAdd">
            <el-form :rules="rule1" ref="form2" :model="form2" status-icon label-position="left" label-width="150px" style='width: 400px; margin-left:50px;'>
                <el-form-item label="支付通道编号: " prop="payChannelNo">
                    <el-input v-model="form2.payChannelNo" disabled=""></el-input>
                </el-form-item>
                <el-form-item label="业务类型： " prop="tradeType" >
                    <el-select class="filter-item" v-model="form2.tradeType" placeholder="请选择" v-if="interfaceDialogStatus === 'update'" disabled="">
                        <el-option label="支付" value="00"> </el-option>
                        <el-option label="支付查询" value="01"> </el-option>
                        <el-option label="退款" value="02"> </el-option>
                        <el-option label="退款查询" value="03"> </el-option>
                        <el-option label="代付" value="04"> </el-option>
                        <el-option label="代付查询" value="05"> </el-option>
                    </el-select>

                    <el-select class="filter-item" v-model="form2.tradeType" placeholder="请选择" v-if="interfaceDialogStatus === 'create'" clearable="">
                        <el-option label="支付" value="00"> </el-option>
                        <el-option label="支付查询" value="01"> </el-option>
                        <el-option label="退款" value="02"> </el-option>
                        <el-option label="退款查询" value="03"> </el-option>
                        <el-option label="代付" value="04"> </el-option>
                        <el-option label="代付查询" value="05"> </el-option>
                    </el-select>
                </el-form-item>      
                <el-form-item label="接口名称: " prop="serviceName">
                    <el-input v-model="form2.serviceName"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogInterfaceAdd = false">取消</el-button>
                <el-button v-if="interfaceDialogStatus === 'create'" type="primary" @click="createInterface('form2')">保存</el-button>
                <el-button v-else-if="interfaceDialogStatus === 'update'" type="primary" @click="updateInfo('form2')">保存</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<style>

</style>

<script>
import { allChannel, add, info, update, deleteObj, interfaceInfo, updateInterface, deleteInterFace, addInterFace } from '@/api/pay/channel'
import waves from '@/directive/waves' // 水波纹指令
export default {
  directives: { waves },
  data: function() {
    return {
      listQuery: {
        payChannelName: '',
        payChannelNo: '',
        page: 1,
        limit: 10
      },
      channel_btn_info: false,
      channel_btn_add: false,
      channel_btn_update: false,
      channel_btn_delete: false,
      channel_btn_interface: false,
      list: [],
      form: {
        payChannelNo: '',
        payChannelName: '',
        payCompanyNo: '',
        payCompanyName: '',
        tradeType: '1'
      },
      form1: {
        payChannelNo: '',
        list: []
      },
      form2: {
        payChannelNo: '',
        tradeType: '',
        serviceName: ''
      },
      rule1: {
        tradeType: [
          {
            required: true,
            message: '请选择支付类型',
            trigger: 'blur'
          }
        ],
        serviceName: [
          {
            required: true,
            message: '请输入接口名称',
            trigger: 'blur'
          },
          {
            pattern: /^\w+$/,
            message: '输入格式错误，请重新输入!'
          }
        ]
      },
      rules: {
        payChannelNo: [
          {
            required: true,
            message: '请输入支付通道编号',
            trigger: 'blur'
          }
        ],
        payChannelName: [
          {
            required: true,
            message: '请输入支付通道名称',
            trigger: 'blur'
          }
        ],
        payCompanyNo: [
          {
            required: true,
            message: '请输入支付公司编号',
            trigger: 'blur'
          }
        ],
        tradeType: [
          {
            required: true,
            message: '请选择业务类型',
            trigger: 'blur'
          }
        ]
      },
      total: undefined,
      listLoading: false,
      dialogFormVisible: false,
      dialogInterface: false,
      dialogInterfaceAdd: false,
      dialogStatus: '',
      interfaceDialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建',
        interface: '接口配置'
      }
    }
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleAdd() {
      this.resetTemp1()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleAddInterFace(payChannelNo) {
      this.resetTemp2()
      this.interfaceDialogStatus = 'create'
      this.form2.payChannelNo = payChannelNo
      this.dialogInterfaceAdd = true
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    getList() {
      this.listLoading = true
      allChannel(this.listQuery)
        .then(response => {
          console.log(response)
          this.list = response.data.rows
          this.total = response.data.total
          this.listLoading = false
        }).catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },

    handleUpdate(row) {
      info(row)
        .then(response => {
          if (response.success) {
            this.dialogStatus = 'update'
            this.dialogFormVisible = true
            this.form = response.info
          }
        })
    },
    handleUpdateInterFace(row) {
      this.form2 = row
      this.interfaceDialogStatus = 'update'
      this.dialogInterfaceAdd = true
    },
    handleDelete(row) {
      this.$confirm('此操作将删除选中支付通道, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteObj(row)
            .then(response => {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              })
              this.getList()
            })
        })
    },
    handleDeleteInterFace(row) {
      this.$confirm('此操作将删除选中支付通道接口, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteInterFace(row)
            .then(response => {
              if (response.success) {
                this.dialogInterface = false
                this.$notify({
                  title: '成功',
                  message: '删除成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              }
            })
        })
    },
    handleInterface(row) {
      this.form1.payChannelNo = row.payChannelNo
      interfaceInfo(row)
        .then(response => {
          this.dialogStatus = 'interface'
          this.dialogInterface = true
          this.form1.list = response.list
        })
    },
    createInterface(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          this.$confirm('此操作将添加支付通道接口, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.listLoading = true
            addInterFace(this.form2).then((response) => {
              if (response.success) {
                this.dialogInterface = false
                this.dialogInterfaceAdd = false
                this.$notify({
                  title: '成功',
                  message: '创建成功',
                  type: 'success',
                  duration: 5000
                })
                this.getList()
              }
              this.listLoading = false
            })
          })
        } else {
          return false
        }
      })
    },
    create(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          this.$confirm('此操作将添加支付通道, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.listLoading = true
            this.dialogFormVisible = false
            add(this.form).then((response) => {
              if (response.success) {
                this.$notify({
                  title: '成功',
                  message: '创建成功',
                  type: 'success',
                  duration: 5000
                })
                this.getList()
              }
              this.listLoading = false
            })
            this.form.resetTemp1()
          })
        } else {
          return false
        }
      })
    },
    update(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false
          update(this.form).then(response => {
            if (response.success) {
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              })
              this.getList()
            }
          })
        } else {
          return false
        }
      })
    },
    updateInfo(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          this.dialogInterface = false
          updateInterface(this.form2).then(response => {
            if (response.success) {
              this.dialogInterface = false
              this.dialogInterfaceAdd = false
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              })
              this.getList()
            }
          })
        } else {
          return false
        }
      })
    },
    resetTemp1() {
      this.form = {
        payChannelNo: undefined,
        payChannelName: undefined,
        payCompanyNo: undefined,
        feeRatio: undefined,
        status: '0',
        businessType: '1'
      }
      this.form1 = {
        payChannelNo: undefined,
        payServiceName: undefined,
        payQueryServiceName: undefined,
        refundServiceName: undefined,
        refundQueryServiceName: undefined,
        payforanotherServiceName: undefined,
        payforanotherQueryServiceName: undefined
      }
    },
    resetTemp2() {
      this.form2 = {
        tradeType: '',
        serviceName: ''
      }
    }
  },
  created() {
    this.getList()
  }
}
</script>

